package com.gzdh.dishrecognition.ui.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.gzdh.dishrecognition.R;
import com.gzdh.dishrecognition.entity.MacMainpSbruanDinnerEntity;

import java.util.ArrayList;
import java.util.List;

public class LeftBmcmAdapter extends RecyclerView.Adapter<LeftBmcmAdapter.ViewHolder> {


    private List<MacMainpSbruanDinnerEntity> list;

    private int select = -1;

    private UserOnClick userOnClick;
    //    LinearLayout mainLinear;
//    TextView nameTv;
    private final Context mContext;

    public LeftBmcmAdapter(Context context) {
        this.mContext = context;
        this.list = new ArrayList<>();
    }

//    private View getViews() {
//        mainLinear = new LinearLayout(mContext);
//        mainLinear.setId(View.generateViewId());
//        mainLinear.setTag("mainLinear");
//        mainLinear.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
//        mainLinear.setPadding(0, 10, 0, 10);
//
//        nameTv = new TextView(mContext);
//        nameTv.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
//        nameTv.setTextSize(TypedValue.COMPLEX_UNIT_SP, 25);
//        nameTv.setTextColor(Color.BLACK);
//        mainLinear.addView(nameTv);
//        return mainLinear;
//    }

    public void setUserOnClick(UserOnClick userOnClick) {
        this.userOnClick = userOnClick;
    }

    @SuppressLint("NotifyDataSetChanged")
    public void setList(List<MacMainpSbruanDinnerEntity> list) {
        this.list = list;
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
//        View view = getViews();
        View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_str2, parent, false);
        return new ViewHolder(itemView);
    }

    @SuppressLint("NotifyDataSetChanged")
    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, @SuppressLint("RecyclerView") int position) {
        holder.nameTv.setText(list.get(position).getDinnerName().trim());
        if (position == select) {
            holder.mainLinear.setBackgroundColor(Color.argb(100, 80, 80, 80));
        } else {
            holder.mainLinear.setBackgroundColor(Color.argb(0, 255, 255, 255));
        }
        holder.mainLinear.setOnClickListener(view -> {
            select = position;
            if (userOnClick != null) {
                userOnClick.userOnClick(list.get(position));
            }
            notifyDataSetChanged();
        });
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        LinearLayout mainLinear;
        TextView nameTv;

        ViewHolder(View item) {
            super(item);
            mainLinear = item.findViewById(R.id.mainLinear);
            nameTv = item.findViewById(R.id.nameTv);
        }
    }

    public interface UserOnClick {
        void userOnClick(MacMainpSbruanDinnerEntity bmcmEntity);
    }

    public MacMainpSbruanDinnerEntity getDinner() {
        try {
            return list.get(select);
        } catch (Exception e) {
            return null;
        }
    }
}
